package com.api.dao.service;

import com.api.dao.DPage;
import com.api.dao.entities.BasicEntities;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
import com.baomidou.mybatisplus.extension.service.IService;

import java.util.List;

/**
 * @author 李大牛家的牛 (scafel@163.com)
 * @product IntelliJ IDEA
 * @createDate 2025/5/20-13:53
 * @description TODO...
 **/
public interface BasicService<T extends BasicEntities> extends IService<T> {
    /**
     * 默认分页大小
     */
    int PAGE_SIZE = 20;

    default T getOneByField(SFunction<T,?> column, Object value, boolean validStatus) {
        LambdaQueryWrapper<T> query = new LambdaQueryWrapper<>();
        query.eq(column, value);
        if (validStatus){
            query.eq(T::getStatus, 1);
        }
        return this.getBaseMapper().selectOne(query);
    }

    default List<T> getListByField(SFunction<T,?> column, Object value, boolean validStatus) {
        LambdaQueryWrapper<T> query = new LambdaQueryWrapper<>();
        query.eq(column, value);
        if (validStatus){
            query.eq(T::getStatus, 1);
        }
        return this.getBaseMapper().selectList(query);
    }

    default DPage<T> getPageByField(SFunction<T,?> column, Object value, boolean validStatus) {
        LambdaQueryWrapper<T> query = new LambdaQueryWrapper<>();
        query.eq(column, value);
        if (validStatus){
            query.eq(T::getStatus, 1);
        }
        return this.getBaseMapper().selectPage(new DPage<>(1, PAGE_SIZE), query);
    }
}